package org.basis.algorithm.sort;

import org.basis.algorithm.common.SortUtil;

import java.util.Arrays;

/**
 * 冒泡排序
 *
 * @author wenpan 2024/03/03 23:23
 */
public class BubbleSort {

    public static void main(String[] args) {

        int[] arr = {6, 7, 1, 3, 4, 5};
        process1(arr);
        SortUtil.printArr(arr);

        final int count = 1000;
        for (int i = 0; i < count; i++) {
            int[] nums = SortUtil.generateRandomArray(100, 100);
            int[] copyArray = SortUtil.copyArray(nums);
            process1(nums);
            Arrays.sort(copyArray);

            // 对数器对数
            if (!SortUtil.isEqual(nums, copyArray)) {
                System.out.println("结果不对.....");
                break;
            }
        }

    }

    public static void process1(int[] nums) {
        for (int i = nums.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }
    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
